package com.zx.role.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zx.role.aop.ZxLog;
import com.zx.role.config.R;
import com.zx.role.domain.*;
import com.zx.role.service.SysDeptService;
import com.zx.role.service.SysMenuService;
import com.zx.role.service.SysRoleService;
import com.zx.role.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysDeptService sysDeptService;

    @Autowired
    SysUserRoleService sysUserRoleService;



    @PostMapping("getRoleList")
    @ZxLog(name = "角色列表")
    public R getRoleList(@RequestBody SysRole sysRole){
        PageHelper.startPage(sysRole.getPageNum(),sysRole.getPageSize());
        List<SysRole> sysRoleList = sysRoleService.selectObjects(sysRole);
        PageInfo pageInfo = new PageInfo(sysRoleList);
        return R.successed(pageInfo);
    }


    /**
     * 获取菜单列表
     * @return
     */
    @GetMapping("getMenuList")
    @ZxLog(name = "菜单列表")
    public R getMenuList(){
        List<SysMenu> sysMenuList = sysMenuService.getMenuList();
        return R.successed(sysMenuList);
    }

    /**
     * 获取部门列表
     */
    @ZxLog(name = "部门列表")
    @GetMapping("getDeptList")
    public R getDeptList(){
        List<SysDept> sysDeptList = sysDeptService.getDeptList();
        return R.successed(sysDeptList);
    }

    /**
     * 获取用户列表
     */
    @PostMapping("getUserByRoleId")
    @ZxLog(name = "已授权用户列表")
    public R getUserByRoleId(@RequestBody SysUser sysUser){
        PageHelper.startPage(sysUser.getPageNum(),sysUser.getPageSize());
        List<SysUser> sysUserList = sysRoleService.getUserByRoleId(sysUser);
        PageInfo pageInfo = new PageInfo<>(sysUserList);
        return R.successed(pageInfo);
    }

    /**
     * 获取未授权用户列表
     */
    @PostMapping("getUserListByRoleId")
    @ZxLog(name = "未授权用户列表")
    public R getUserListByRoleId(@RequestBody SysUser sysUser){
        PageHelper.startPage(sysUser.getPageNum(),sysUser.getPageSize());
        List<SysUser> sysUserList = sysRoleService.getUserListByRoleId(sysUser);
        PageInfo pageInfo = new PageInfo<>(sysUserList);
        return R.successed(pageInfo);
    }

    /**
     * 取消用户授权
     */
    @PostMapping("DelUserRole")
    @ZxLog(name = "取消用户授权")
    public R DelUserRole(@RequestBody SysUserRole sysUserRole){
        boolean b= sysUserRoleService.deleteObject(sysUserRole);
        return b==true?R.successed(null):R.failed("取消授权失败");
    }

    /**
     * 修改角色状态
     */
    @PostMapping("updateRoleStatusById")
    @ZxLog(name = "修改用户状态")
    public R updateRoleStatusById(@RequestBody SysRole sysRole){
        int i = sysRoleService.updateObject(sysRole);
        return i>0? R.successed(null): R.failed("修改失败");
    }

    /**
     * 批量取消用户授权
     */
    @PostMapping("bathDelUserRole")
    @ZxLog(name = "批量取消用户授权")
    private R bathDelUserRole(@RequestBody SysRole sysRole){
//        Integer[] haveIds = sysRole.getHaveIds();
//        List<SysUserRole> list = new ArrayList<>();
//        for (Integer haveId : haveIds) {
//            SysUserRole sysUserRole = new SysUserRole();
//            sysUserRole.setRoleId(Long.valueOf(sysRole.getHaveId()));
//            sysUserRole.setUserId(Long.valueOf(haveId));
//            list.add(sysUserRole);
//        }
       boolean b = sysUserRoleService.batchDelUserRole(sysRole);
        return b==true?R.successed(null):R.failed("取消授权失败");
    }

    /**
     * 批量授权
     */
    @PostMapping("batchSaveUserRole")
    @ZxLog(name = "批量授权")
    public R batchSaveUserRole(@RequestBody SysRole sysRole){
        Integer[] haveIds = sysRole.getHaveIds();
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (Integer haveId  : haveIds) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(Long.valueOf(sysRole.getHaveId()));
            sysUserRole.setUserId(Long.valueOf(haveId));
            sysUserRoleList.add(sysUserRole);
        }
        boolean b = sysUserRoleService.insertObjectBach(sysUserRoleList);
        return b==true?R.successed(null):R.failed("添加授权失败");
    }

    /**
     * 单删（逻辑）
     */
    @DeleteMapping("delRole")
    @ZxLog(name = "单删（逻辑）")
    public R delRole(Integer id){
        boolean b = sysRoleService.deleteObjectOnly(id);
        return b==true?R.successed(null):R.failed("删除失败");
    }

    /**
     * 批删（逻辑）
     */
    @ZxLog(name = "批删（逻辑）")
    @PostMapping("bathDelRole")
    public R bathDelRole(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.bathDelRole(sysRole.getHaveIds());
        return b==true?R.successed(null):R.failed("删除失败");
    }

    /**
     * 统计每个角色分配的用户数
     */
    @GetMapping("getUserCountByRole")
    public R getUserCountByRole(){
        Map<String,Object> map = new HashMap<>();
        List<String> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        List<SysRole> sysRoleList = sysRoleService.getUserCountByRole();
        for (SysRole sysRole : sysRoleList) {
            x.add(sysRole.getRoleName());
            y.add(sysRole.getHaveId());
        }
        map.put("x",x);
        map.put("y",y);
        return R.successed(map);
    }

    /**
     * 统计每个用户拥有的角色数
     */
    @GetMapping("getRoleCountByUser")
    public R getRoleCountByUser(){
        List<BingInfo> bingInfoList = new ArrayList<>();
        List<SysUser> sysUserList = sysRoleService.getRoleCountByUser();
        for (SysUser sysUser : sysUserList) {
            BingInfo bingInfo = new BingInfo();
            bingInfo.setName(sysUser.getUserName());
            bingInfo.setValue(sysUser.getHaveId());
            bingInfoList.add(bingInfo);
        }
        return R.successed(bingInfoList);
    }

}
